<?php

namespace app\api\controller;

use app\common\controller\Common;

class Novel extends Common
{
    /**
     * 小说详情
     */
    public function detail()
    {
        $primary_id = trim($this->request->param('primary_id', 0));

        $novel = model("article")->where('PrimaryId','eq',(int)$primary_id)->cache()->find();
        if (!empty($novel)){
            $novel = $novel->toArray();
        }
        $all_chapter = get_all_chapter_list($novel['_id'],$primary_id,$novel['SourceList'],0,0);

        $novel['ChapterTotal'] = $all_chapter['chapter_total'];
        $novel['LastChapterSort'] = $all_chapter['last_chapter_sort'];
        $novel['LastChapterTitle'] = $all_chapter['last_chapter_title'];
        $novel['LastChapterFlag'] = $all_chapter['last_chapter_flag'];
        $novel['LastUpdateChapterList'] = $all_chapter['last_update_chapter_list'];
        $novel['AllChapterList'] = $all_chapter['chapter_list'];

        unset($novel['_id']);

        return $this->echoJson($novel);
    }

    /**
     * 相关推荐
     * @throws
     */
    public function commend_list()
    {
        $primary_id = trim($this->request->param('primary_id', 0));
        $commendList = model('article')->whereIn('Cid',get_channel_all_sub_cid_list('winnow'))->limit(0,8)->order('PrimaryId','desc')->cache()->select();
        foreach ($commendList as &$item) {
            $item['Id'] = $item['_id']->__toString();
            unset($item['_id']);
        }
        return $this->echoJson($commendList);
    }

    /**
     * 搜索
     * @throws
     */
    public function search()
    {
        $keyword = trim($this->request->param('keyword', ""));

        $list = model('article')->whereIn('Cid',get_all_sub_cid_list())->whereOr('Title','like',$keyword)->whereOr('Author','like',$keyword)->order('UpdateTime','desc')->order('PrimaryId','desc')->cache()->select();
        foreach ($list as &$item) {
            $item['Id'] = $item['_id']->__toString();
            unset($item['_id']);
        }
        return $this->echoJson($list);
    }

}